﻿using System;
using System.Web;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DB_QLSV;
using DB_QLSV.BusinessClassLibrary;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Diagnostics;


namespace GUI_QLSV
{
    public partial class DiemSo : UserControl
    {
        private int pre_row = -1;
        private int pre_column = -1;
        private String MaMonHoc = "";
        private String MaLop = "";
        public static List<BangDiem> cur_DSDiem = new List<BangDiem>();
        public static List<BangDiem> cur_DSDiemNull = new List<BangDiem>();

        public static List<Lop> cur_DSClass = new List<Lop>();
        public static List<Lop> cur_DSClassNull = new List<Lop>();

        public static List<BangDiem2> cur_DSDiemTabStudent = new List<BangDiem2>();
        public static List<BangDiem2> cur_DSDiemTabStudentNull = new List<BangDiem2>();

        private List<BangDiem2> cur_DSDiem2 = new List<BangDiem2>();
        private int tieuchi1 = -1;
        private int tieuchi2 = -1;
        public static TreeNode selectedNodeInBranch = new TreeNode();
        public static Lop _classObject = new Lop();
        public static List<BangDiem> ScoreOFStudentData_InDGV = new List<BangDiem>();
        public static List<BangDiem2> ScoreOFStudentData_InDGVTabStudent = new List<BangDiem2>();
        public DiemSo()
        {
            InitializeComponent();
            //to load search conditions for comboboxSearch on Class tab
            List<StudentManagement.SearchButtonObject> lst = StudentManagement.ScoreClass.initButtonSearchOnTabClass();
            foreach (StudentManagement.SearchButtonObject item in lst)
            {
                comboBox_TieuChi.Items.Add(item);
            }
            comboBox_TieuChi.DisplayMember = "ten";
            comboBox_TieuChi.ValueMember = "id";
            comboBox_TieuChi.SelectedIndex =1;

            //to load search conditions for comboboxSearch on student tab
            List<StudentManagement.SearchButtonObject> list = StudentManagement.ScoreClass.initButtonSearchOnTabStudent();
            foreach (StudentManagement.SearchButtonObject item in list)
            {
                comboBox_TieuChi2.Items.Add(item);
            }
            comboBox_TieuChi2.DisplayMember = "ten";
            comboBox_TieuChi2.ValueMember = "id";
            comboBox_TieuChi2.SelectedIndex = 1;
            dataGridView_BangDiem2.DataSource = cur_DSDiemTabStudentNull;
            dataGridView_Lop.DataSource = cur_DSDiemTabStudentNull;
        }

        public void ResetTreeKhoa()
        {
            foreach (TreeNode tn in treeView_Khoa.Nodes[0].Nodes)
            {
                tn.ForeColor = Color.Black;
            }
        }
        public void ResetTreeNganh()
        {
            foreach (TreeNode tn in treeView_Nganh.Nodes[0].Nodes)
            {
                tn.ForeColor = Color.Black;
            }
        }

        private void DiemSo_Load(object sender, EventArgs e)
        {            
            TreeNode node = new TreeNode();
            node = treeView_Khoa.Nodes.Add("0","Khoa");
            List<Khoa> DSKhoa = DB_QLSV.BusinessClassLibrary.KhoaBUS.LayDanhSach();
            foreach (Khoa item in DSKhoa)
            {
                node.Nodes.Add(item.masokhoa, item.tenkhoa);
            }
            treeView_Khoa.SelectedNode = node;
            node = treeView_Nganh.Nodes.Add("", "Ngành");
            treeView_Khoa.ExpandAll();                                  
        }

        private void treeView_Khoa_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            dataGridView_Lop.DataSource = cur_DSClassNull;
            ResetTreeKhoa();
            treeView_Nganh.Nodes.Clear();
            TreeNode node = new TreeNode();
            node = treeView_Nganh.Nodes.Add("", "Ngành");
            String str_makhoa = e.Node.Name;
            List<Nganh> DSNganh = NganhBUS.LayDanhSachTheoMaNganh(str_makhoa);            
            if (DSNganh == null)
                return;
            foreach (Nganh item in DSNganh)
            {
                node.Nodes.Add(item.masonganh,item.tennganh);
            }
            e.Node.ForeColor = Color.Red;
            treeView_Nganh.ExpandAll();                     
        }

        private void treeView_Nganh_Click(object sender, EventArgs e)
        {
            textBox_TimKiem.Text = "Tìm kiếm";
        }

        private void treeView_Khoa_Click(object sender, EventArgs e)
        {
            textBox_TimKiem.Text = "Tìm kiếm";
            dataGridView_Lop.DataSource = cur_DSClassNull;
            dataGridView_BangDiem.DataSource = cur_DSDiemNull;
            ScoreOFStudentData_InDGV = cur_DSDiemNull;
            cur_DSDiem = cur_DSDiemNull;
        }

        private void treeView_Nganh_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            selectedNodeInBranch = treeView_Nganh.GetNodeAt(e.X, e.Y);
            ResetTreeNganh();
            e.Node.ForeColor = Color.Red;
            String manganh = e.Node.Name;
            List<Lop> DSLop = LopBUS.LayDanhSachTheoMaNganh(manganh);
            if (DSLop == null)
                return;
            dataGridView_Lop.DataSource = DSLop;
        }

        private void dataGridView_Lop_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            textBox_TimKiem.Text = "Tìm kiếm";
            comboBox_MonHoc.DataSource = null;
            dataGridView_Lop.DefaultCellStyle.BackColor = Color.White;
            dataGridView_Lop.DefaultCellStyle.ForeColor = Color.Black;
            int row = e.RowIndex;   
            int column = e.ColumnIndex;
            if (row < 0 || column < 0)
                return;
            String malop = dataGridView_Lop.Rows[row].Cells["malop2"].Value.ToString();
            _classObject = LopBUS.getClassObject(malop);
            dataGridView_Lop.Rows[row].DefaultCellStyle.BackColor = Color.Green;
            dataGridView_Lop.Rows[row].DefaultCellStyle.ForeColor = Color.White;
            if (pre_row != -1 && pre_row != row)
            {
                dataGridView_Lop.Rows[pre_row].DefaultCellStyle.BackColor = Color.White;
                dataGridView_Lop.Rows[pre_row].DefaultCellStyle.ForeColor = Color.Black;
            }
            pre_row = row;
            MaLop = dataGridView_Lop.Rows[row].Cells["malop2"].Value.ToString();

            List<MonHoc> DSMonHoc = MonHocBUS.LayDanhSachTheoMaSoLop(MaLop);
            if (DSMonHoc == null)
                return;
            comboBox_MonHoc.DataSource = DSMonHoc;
            comboBox_MonHoc.ValueMember = "mamonhoc";
            comboBox_MonHoc.DisplayMember = "tenmonhoc";
        }

        private void comboBox_MonHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox_TimKiem.Text = "Tìm kiếm";
            if (dataGridView_BangDiem.RowCount > 0)
            {
                dataGridView_BangDiem.DataSource = cur_DSDiemNull;
                ScoreOFStudentData_InDGV = cur_DSDiemNull;
            }
            if (comboBox_MonHoc.SelectedValue == null)
                return;
            MaMonHoc = comboBox_MonHoc.SelectedValue.ToString();
            List<BangDiem> DSDiem = new List<BangDiem>();
            DSDiem = DiemBUS.LayBangDiemTheoLopVaMonHoc(MaLop, MaMonHoc);            
            if (DSDiem == null)
                return;            
            cur_DSDiem = DSDiem;

            if (cur_DSDiem == null)
            {
                dataGridView_BangDiem.DataSource = cur_DSDiemNull;
                ScoreOFStudentData_InDGV = cur_DSDiemNull;
            }
            else
            {
                dataGridView_BangDiem.DataSource = cur_DSDiem;
                ScoreOFStudentData_InDGV = cur_DSDiem;
            }



        }

        private void textBox_TimKiem_Click(object sender, EventArgs e)
        {
            textBox_TimKiem.SelectAll();
        }


        private void textBox_TimKiem_MouseClick(object sender, MouseEventArgs e)
        {
            textBox_TimKiem.Text = ""; ;
        }

        private void textBox_TimKiem2_TextChanged(object sender, EventArgs e)
        {
            string searchText = textBox_TimKiem2.Text;
            int index = comboBox_TieuChi2.SelectedIndex;
            List<BangDiem2> tmp = new List<BangDiem2>();
            tmp = StudentManagement.MonHocClass.getAllStudent_DependOn_StudentId(cur_DSDiemTabStudent, searchText, index);
            ScoreOFStudentData_InDGVTabStudent = tmp;
            if (tmp == null)
                dataGridView_BangDiem2.DataSource = cur_DSDiemTabStudentNull;
            else
                dataGridView_BangDiem2.DataSource = ScoreOFStudentData_InDGVTabStudent;
        }

        private void textBox_TimKiem2_MouseClick(object sender, MouseEventArgs e)
        {
            textBox_TimKiem2.Text="";
        }

        private void textBox_MSSV_MouseClick(object sender, MouseEventArgs e)
        {
            textBox_MSSV.Text = "";
        }


        private void comboBoxTieuDe2_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox_TimKiem2.Text = "Tìm kiếm";
            if (cur_DSDiem == null)
            {
                dataGridView_BangDiem2.DataSource = cur_DSDiemTabStudentNull;
                ScoreOFStudentData_InDGVTabStudent = cur_DSDiemTabStudentNull;
            }
            else
            {
                dataGridView_BangDiem2.DataSource = cur_DSDiemTabStudent;
                ScoreOFStudentData_InDGVTabStudent = cur_DSDiemTabStudent;
            }
        }

        private void textBox_TimKiem2_KeyDown(object sender, KeyEventArgs e)
        {
           
            if (e.KeyCode == Keys.Enter)
            {
                textBox_TimKiem2.SelectAll();
                List<BangDiem2> temp = new List<BangDiem2>();
                foreach (BangDiem2 item in cur_DSDiem2)
                {
                    if (tieuchi2 == 0)
                    {
                        if (item.mamonhoc.Contains(textBox_TimKiem2.Text.ToString()))
                        {
                            temp.Add(item);
                        }
                    }
                    if (tieuchi2 == 1)
                    {
                        if (item.tenmonhoc.Contains(textBox_TimKiem2.Text.ToString()))
                        {
                            temp.Add(item);
                        }
                    }
                }
                dataGridView_BangDiem2.DataSource = temp;
                dataGridView_BangDiem2.Columns["mssv"].HeaderText = "MSSV";
                dataGridView_BangDiem2.Columns["mssv"].Width = 178;
                dataGridView_BangDiem2.Columns["tensv"].HeaderText = "Tên SV";
                dataGridView_BangDiem2.Columns["tensv"].Width = 178;
                dataGridView_BangDiem2.Columns["mamonhoc"].HeaderText = "Mã Môn Học";
                dataGridView_BangDiem2.Columns["mamonhoc"].Width = 178;
                dataGridView_BangDiem2.Columns["tenmonhoc"].HeaderText = "Tên Môn Học";
                dataGridView_BangDiem2.Columns["tenmonhoc"].Width = 178;
                dataGridView_BangDiem2.Columns["diemgiuaky"].HeaderText = "Điểm Giữa Kỳ";
                dataGridView_BangDiem2.Columns["diemgiuaky"].Width = 178;
                dataGridView_BangDiem2.Columns["diemcuoiky"].HeaderText = "Điểm Cuối Kỳ";
                dataGridView_BangDiem2.Columns["diemcuoiky"].Width = 178;
                dataGridView_BangDiem2.Columns["diemtrungbinh"].HeaderText = "Điểm Trung Bình";
                dataGridView_BangDiem2.Columns["diemtrungbinh"].Width = 178;

            }
        }

        private void button10_Click_1(object sender, EventArgs e)
        {
            textBox_TimKiem2.Text = "";
            textBox_MSSV.Text = "";
            comboBox_TieuChi2.SelectedIndex = -1;
            dataGridView_BangDiem2.DataSource = cur_DSDiemTabStudentNull;
            ScoreOFStudentData_InDGVTabStudent = cur_DSDiemTabStudentNull;
        }

        private void textBox_TimKiem_TextChanged(object sender, EventArgs e)
        {

            string searchText = textBox_TimKiem.Text;
            int index = comboBox_TieuChi.SelectedIndex;
            List<BangDiem> tmp = new List<BangDiem>();
            tmp = StudentManagement.MonHocClass.getAllScoreOfStudent_DependOn_Search(cur_DSDiem, searchText, index);
            ScoreOFStudentData_InDGV = tmp;
            if (tmp == null)
                dataGridView_BangDiem.DataSource = cur_DSDiemNull;
            else
                dataGridView_BangDiem.DataSource = tmp;
        }

        private void textBox_TimKiem_Click_1(object sender, EventArgs e)
        {
            textBox_TimKiem.Text = "";
        }

        private void comboBox_TieuChi_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox_TimKiem.Text = "Tìm kiếm";
            if (cur_DSDiem == null)
            {
                dataGridView_BangDiem.DataSource = cur_DSDiemNull;
                ScoreOFStudentData_InDGV = cur_DSDiemNull;
            }
            else
            {
                dataGridView_BangDiem.DataSource = cur_DSDiem;
                ScoreOFStudentData_InDGV = cur_DSDiem;
            }
        }
        

        private void treeView_Nganh_MouseClick(object sender, MouseEventArgs e)
        {
            selectedNodeInBranch = treeView_Nganh.GetNodeAt(e.X, e.Y);
        }
        private void bttPrint_Click(object sender, EventArgs e)
        {
            //create folder PDFPrint
            String bin = Directory.GetCurrentDirectory();
            int index = bin.IndexOf("bin");
            int iIndex = bin.IndexOf("DemoQLSV");
            string tmp = bin.Substring(0, iIndex);
            string tmpImage = tmp + @"\DemoQLSV\images\02_calendar.png";
            tmp = tmp + @"DemoQLSV\GUI_QLSV\PrintPDF\make.bat";
            ProcessStartInfo p = new ProcessStartInfo(tmp);
            Process pro = new Process();
            pro.StartInfo = p;
            pro.Start();
            pro.WaitForExit();
            //create file pdf
            var doc = new Document();
            string path = @"C:\PDFPrint\";
            string name = System.DateTime.Now.ToUniversalTime().ToString();
            // PDF14304010022011
            DateTime dt = DateTime.Now;
            string filename = "PDF__SCOREOFSTU_" + dt.Hour + dt.Minute + dt.Second + "_" + dt.Day + dt.Month + dt.Year + ".pdf";
            //show time GMT time when PDF created
            //set header [1] text [2] font stype
            Phrase phase = new Phrase(DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss") + " GMT", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 8));
            PdfWriter.GetInstance(doc, new FileStream(path + filename, FileMode.Create));
            BaseFont baseFont = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, true);
            doc.Open();

            iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(tmpImage);
            gif.Alignment = iTextSharp.text.Image.LEFT_ALIGN;
            gif.ScalePercent(50f);
            doc.Add(gif);
            //to write data from dataGV into pdf file
            PdfPTable pdfHeader = new PdfPTable(2);
            pdfHeader.LockedWidth = true;
            pdfHeader.TotalWidth = 240f;
            float[] widths = new float[2];
            widths[0] = 90f;
            widths[1] = 130F;
            pdfHeader.SetWidths(widths);

            //pdf cell
            PdfPCell pdfCell = null;
            //add header of the pdf table
            // c.1. Add Header of the pdf table
            // Font fontTitle = new Font(baseFont, 20f, Element.ALIGN_LEFT, new Color(216, 37, 33));
            iTextSharp.text.Font fontTitle = new iTextSharp.text.Font(baseFont, 20f, Element.ALIGN_CENTER, new iTextSharp.text.BaseColor(216, 37, 33));

            iTextSharp.text.Font font1 = new iTextSharp.text.Font(baseFont, 10f);

            //pdfCell = new PdfPCell(new Phrase(new Chunk("Trường Đại Học Khoa Học Tự Nhiên Thành Phố Hồ Chí Minh ", font1)));
            //pdfCell.FixedHeight = 16f;
            //pdfCell.BorderWidth = 0;
            //pdfHeader.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("DANH SÁCH ĐIỂM CỦA HỌC SINH", fontTitle)));
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell.Colspan = 2;
            pdfCell.FixedHeight = 80f;
            pdfCell.BorderWidth = 0;
            //  pdfCell.PaddingTop = -2f;
            pdfHeader.AddCell(pdfCell);

            // c.2. Add Header row

            // Row 1
            pdfCell = new PdfPCell(new Phrase(new Chunk("Ngành học", font1)));
            //PdfPCell.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfCell.FixedHeight = 16f;
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);

            //Nganh branchObject = (Nganh)selectedNodeInBranch.Tag; 
            string branchObject = selectedNodeInBranch.Text;
            if (branchObject == null || branchObject=="")
            {
                return;
            }
            pdfCell = new PdfPCell(new Phrase(new Chunk(": " + branchObject, font1)));
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);
            string nowDay = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();

            // Row 1
            pdfCell = new PdfPCell(new Phrase(new Chunk("Lớp học", font1)));
            //PdfPCell.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfCell.FixedHeight = 16f;
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);

            // Nganh branchObject = (Nganh)selectedNodeInBranch.Tag;
            if (_classObject == null)
            {
                return;
            }

            pdfCell = new PdfPCell(new Phrase(new Chunk(": " + _classObject.tenlop, font1)));
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);
            // Row 2
            pdfCell = new PdfPCell(new Phrase(new Chunk("Ngày print ", font1)));
            pdfCell.FixedHeight = 16f;
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk(": " + nowDay, font1)));
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);


            // Row 3
            pdfCell = new PdfPCell(new Phrase(new Chunk("Giáo viên ", font1)));
            pdfCell.FixedHeight = 16f;
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);

            // Row 4
            pdfCell = new PdfPCell(new Phrase(new Chunk(": " + "Nguyễn Văn A", font1)));
            pdfCell.FixedHeight = 16f;
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);

            doc.Add(pdfHeader);

            // Add content of gridview
            //Craete instance of the pdf table and set the number of column in that table
            PdfPTable ptbGrid = new PdfPTable(5);
            //fix the absolute width of the table
            ptbGrid.LockedWidth = true;
            ptbGrid.SpacingBefore = 20f;
            ptbGrid.TotalWidth = doc.Right - doc.Left;
            ptbGrid.HorizontalAlignment = Element.ALIGN_RIGHT;
            widths = new float[5];
            widths[0] = 0.6F;
            widths[1] = 1.0F;
            widths[2] = 1.0F;
            widths[3] = 1.0F;
            widths[4] = 1.0F;
            ptbGrid.SetWidths(widths);

            pdfCell = null;
            BaseFont bf1 = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, true);
            iTextSharp.text.Font fontGrid = new iTextSharp.text.Font(baseFont, 8);

            //Add Header of the pdf table
            pdfCell = new PdfPCell(new Phrase(new Chunk("MSSV", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.FixedHeight = 16f;
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Họ Tên", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Điểm Giữa Kỳ", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Điểm Cuối Kỳ", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Điểm Trung Bình", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            //  
            //How add the data from datatable to pdf table
            int i = 1;
            int iNumOfItem = 0;
            if (ScoreOFStudentData_InDGV == null)
                return;
            foreach (BangDiem item in ScoreOFStudentData_InDGV)
            {
                //pdfCell = new PdfPCell(new Phrase(new Chunk(i++.ToString(), fontGrid)));
                //pdfCell.FixedHeight = 16f;
                //pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
                //pdfCell.PaddingTop = 3f;
                //ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.mssv, fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.tensv, fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.diemgiuaky.ToString(), fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.diemcuoiky.ToString(), fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.diemtrungbinh.ToString(), fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);
 
            }

            doc.Add(ptbGrid);
            doc.Close();
            Process.Start(path + filename);
        }

        private void textBox_MSSV_TextChanged(object sender, EventArgs e)
        {
            string searchText = textBox_MSSV.Text;

            List<BangDiem2> DSDiem = new List<BangDiem2>();
            DSDiem = DiemBUS.GetStudentList_DependOn_StudentID(searchText);
            if (DSDiem == null)
            {
                cur_DSDiemTabStudent = cur_DSDiemTabStudentNull;
                dataGridView_BangDiem2.DataSource = cur_DSDiemTabStudentNull;
                return;
            }
            cur_DSDiemTabStudent = DSDiem;
            if (cur_DSDiemTabStudent == null)
            {
                dataGridView_BangDiem2.DataSource = cur_DSDiemTabStudentNull;
                ScoreOFStudentData_InDGVTabStudent = cur_DSDiemTabStudentNull;
            }
            else
            {
                dataGridView_BangDiem2.DataSource = cur_DSDiemTabStudent;
                ScoreOFStudentData_InDGVTabStudent = cur_DSDiemTabStudent;
            }
        }

        private void bttPrintStudentId_Click(object sender, EventArgs e)
        {
            //create folder PDFPrint
            String bin = Directory.GetCurrentDirectory();
            int index = bin.IndexOf("bin");
            int iIndex = bin.IndexOf("DemoQLSV");
            string tmp = bin.Substring(0, iIndex);
            string tmpImage = tmp + @"\DemoQLSV\images\02_calendar.png";
            tmp = tmp + @"DemoQLSV\GUI_QLSV\PrintPDF\make.bat";
            ProcessStartInfo p = new ProcessStartInfo(tmp);
            Process pro = new Process();
            pro.StartInfo = p;
            pro.Start();
            pro.WaitForExit();
            //create file pdf
            var doc = new Document();
            string path = @"C:\PDFPrint\";
            string name = System.DateTime.Now.ToUniversalTime().ToString();
            // PDF14304010022011
            DateTime dt = DateTime.Now;
            string filename = "PDF__SCOREOFSTU_STUDENTTAB" + dt.Hour + dt.Minute + dt.Second + "_" + dt.Day + dt.Month + dt.Year + ".pdf";
            //show time GMT time when PDF created
            //set header [1] text [2] font stype
            Phrase phase = new Phrase(DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss") + " GMT", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 8));
            PdfWriter.GetInstance(doc, new FileStream(path + filename, FileMode.Create));
            BaseFont baseFont = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, true);
            doc.Open();

            iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(tmpImage);
            gif.Alignment = iTextSharp.text.Image.LEFT_ALIGN;
            gif.ScalePercent(50f);
            doc.Add(gif);
            //to write data from dataGV into pdf file
            PdfPTable pdfHeader = new PdfPTable(2);
            pdfHeader.LockedWidth = true;
            pdfHeader.TotalWidth = 240f;
            float[] widths = new float[2];
            widths[0] = 90f;
            widths[1] = 130F;
            pdfHeader.SetWidths(widths);

            //pdf cell
            PdfPCell pdfCell = null;
            //add header of the pdf table
            // c.1. Add Header of the pdf table
            // Font fontTitle = new Font(baseFont, 20f, Element.ALIGN_LEFT, new Color(216, 37, 33));
            iTextSharp.text.Font fontTitle = new iTextSharp.text.Font(baseFont, 20f, Element.ALIGN_CENTER, new iTextSharp.text.BaseColor(216, 37, 33));

            iTextSharp.text.Font font1 = new iTextSharp.text.Font(baseFont, 10f);

            //pdfCell = new PdfPCell(new Phrase(new Chunk("Trường Đại Học Khoa Học Tự Nhiên Thành Phố Hồ Chí Minh ", font1)));
            //pdfCell.FixedHeight = 16f;
            //pdfCell.BorderWidth = 0;
            //pdfHeader.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("DANH SÁCH BẢNG ĐIỂM CỦA HỌC SINH", fontTitle)));
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell.Colspan = 2;
            pdfCell.FixedHeight = 80f;
            pdfCell.BorderWidth = 0;
            //  pdfCell.PaddingTop = -2f;
            pdfHeader.AddCell(pdfCell);

            // c.2. Add Header row

            // Row 1
            //pdfCell = new PdfPCell(new Phrase(new Chunk("Ngành học", font1)));
            ////PdfPCell.HorizontalAlignment = Element.ALIGN_LEFT;
            //pdfCell.FixedHeight = 16f;
            //pdfCell.BorderWidth = 0;
            //pdfHeader.AddCell(pdfCell);

            ////Nganh branchObject = (Nganh)selectedNodeInBranch.Tag; 
            //string branchObject = selectedNodeInBranch.Text;
            //if (branchObject == null || branchObject=="")
            //{
            //    return;
            //}
            //pdfCell = new PdfPCell(new Phrase(new Chunk(": " + branchObject, font1)));
            //pdfCell.BorderWidth = 0;
            //pdfHeader.AddCell(pdfCell);

            string nowDay = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();

            // Row 1
            //pdfCell = new PdfPCell(new Phrase(new Chunk("Lớp học", font1)));
            ////PdfPCell.HorizontalAlignment = Element.ALIGN_LEFT;
            //pdfCell.FixedHeight = 16f;
            //pdfCell.BorderWidth = 0;
            //pdfHeader.AddCell(pdfCell);

            //// Nganh branchObject = (Nganh)selectedNodeInBranch.Tag;
            //if (_classObject == null)
            //{
            //    return;
            //}

            //pdfCell = new PdfPCell(new Phrase(new Chunk(": " + _classObject.tenlop, font1)));
            //pdfCell.BorderWidth = 0;
            //pdfHeader.AddCell(pdfCell);
            // Row 2
            pdfCell = new PdfPCell(new Phrase(new Chunk("Ngày print ", font1)));
            pdfCell.FixedHeight = 16f;
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk(": " + nowDay, font1)));
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);


            // Row 3
            pdfCell = new PdfPCell(new Phrase(new Chunk("Giáo viên ", font1)));
            pdfCell.FixedHeight = 16f;
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);

            // Row 4
            pdfCell = new PdfPCell(new Phrase(new Chunk(": " + "Nguyễn Văn A", font1)));
            pdfCell.FixedHeight = 16f;
            pdfCell.BorderWidth = 0;
            pdfHeader.AddCell(pdfCell);

            doc.Add(pdfHeader);

            // Add content of gridview
            //Craete instance of the pdf table and set the number of column in that table
            PdfPTable ptbGrid = new PdfPTable(7);
            //fix the absolute width of the table
            ptbGrid.LockedWidth = true;
            ptbGrid.SpacingBefore = 20f;
            ptbGrid.TotalWidth = doc.Right - doc.Left;
            ptbGrid.HorizontalAlignment = Element.ALIGN_RIGHT;
            widths = new float[7];
            widths[0] = 0.6F;
            widths[1] = 0.7F;
            widths[2] = 0.6F;
            widths[3] = 0.9F;
            widths[4] = 0.6F;
            widths[5] = 0.6F;
            widths[6] = 0.7F;
            ptbGrid.SetWidths(widths);

            pdfCell = null;
            BaseFont bf1 = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, true);
            iTextSharp.text.Font fontGrid = new iTextSharp.text.Font(baseFont, 8);

            //Add Header of the pdf table
            pdfCell = new PdfPCell(new Phrase(new Chunk("MSSV", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.FixedHeight = 16f;
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Họ Tên", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Mã Môn Học", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Tên Môn Học", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Điểm Giữa Kỳ", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Điểm Cuối Kỳ", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            pdfCell = new PdfPCell(new Phrase(new Chunk("Điểm Trung Bình", fontGrid)));
            pdfCell.BackgroundColor = new iTextSharp.text.BaseColor(222, 222, 222);
            pdfCell.PaddingTop = 1f;
            pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptbGrid.AddCell(pdfCell);

            //  
            //How add the data from datatable to pdf table
            int i = 1;
            int iNumOfItem = 0;
            if (ScoreOFStudentData_InDGVTabStudent == cur_DSDiemTabStudentNull)
                return;
            foreach (BangDiem2 item in ScoreOFStudentData_InDGVTabStudent)
            {
                //pdfCell = new PdfPCell(new Phrase(new Chunk(i++.ToString(), fontGrid)));
                //pdfCell.FixedHeight = 16f;
                //pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
                //pdfCell.PaddingTop = 3f;
                //ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.mssv, fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.tensv, fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.mamonhoc, fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.tenmonhoc, fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.diemgiuaky.ToString(), fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.diemcuoiky.ToString(), fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);

                pdfCell = new PdfPCell(new Phrase(new Chunk(item.diemtrungbinh.ToString(), fontGrid)));
                pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
                pdfCell.PaddingTop = 1f;
                ptbGrid.AddCell(pdfCell);
 
            }

            doc.Add(ptbGrid);
            doc.Close();
            Process.Start(path + filename);
        
        }

        

 
    }
}
